// 视频页面使用示例
import { useState, useEffect } from 'react';
import { getCurrentPlayingVideo, getCurrentVideoIndex, scrollToVideo, forceCheckCurrentVideo, getCurrentVideoComments } from './index';

// 示例1: 获取当前播放的视频数据
export const getCurrentVideoData = () => {
  const currentVideo = getCurrentPlayingVideo();

  if (currentVideo) {
    console.log('当前播放的视频信息:', {
      id: currentVideo.id,
      title: currentVideo.title,
      author: currentVideo.author,
      videoUrl: currentVideo.originalUrl,
      duration: currentVideo.duration,
      likeCount: currentVideo.likeCount,
      commentCount: currentVideo.commentCount,
      shareCount: currentVideo.shareCount
    });
    return currentVideo;
  } else {
    console.log('没有正在播放的视频');
    return null;
  }
};

// 示例2: 获取当前播放视频的索引
export const getCurrentVideoIndexData = () => {
  const currentIndex = getCurrentVideoIndex();
  console.log('当前播放视频的索引:', currentIndex);
  return currentIndex;
};

// 示例3: 跳转到指定视频
export const jumpToVideo = (index: number) => {
  console.log(`跳转到第 ${index} 个视频`);
  scrollToVideo(index);
};

// 示例4: 强制检测当前播放视频（当滑动检测失效时使用）
export const forceCheckVideo = () => {
  console.log('强制检测当前播放视频...');
  forceCheckCurrentVideo();

  // 检测后获取结果
  const currentVideo = getCurrentPlayingVideo();
  const currentIndex = getCurrentVideoIndex();

  console.log('强制检测结果:', {
    index: currentIndex,
    video: currentVideo
  });

  return { currentVideo, currentIndex };
};

// 示例5: 获取当前视频的评论数据
export const getCurrentVideoCommentsData = () => {
  const comments = getCurrentVideoComments();

  console.log('当前视频的评论数据:', comments);

  if (comments.length > 0) {
    console.log(`共 ${comments.length} 条评论:`);
    comments.forEach((comment: any, index: number) => {
      console.log(`${index + 1}. [ID: ${comment.id}] ${comment.content}`);
    });
  } else {
    console.log('当前视频暂无评论数据');
  }

  return comments;
};

// 示例6: 监听当前播放视频的变化
export const watchCurrentVideo = () => {
  let lastVideoId: any = null;

  const checkVideoChange = () => {
    const currentVideo = getCurrentPlayingVideo();

    if (currentVideo && currentVideo.id !== lastVideoId) {
      console.log('视频已切换到:', currentVideo.title);
      lastVideoId = currentVideo.id;

      // 获取新视频的评论数据
      const comments = getCurrentVideoComments();
      console.log('新视频的评论数据:', comments);

      // 在这里可以执行其他操作，比如：
      // - 上报播放数据
      // - 更新UI状态
      // - 发送分析事件
    }
  };

  // 每500ms检查一次视频变化
  const interval = setInterval(checkVideoChange, 500);

  // 返回清理函数
  return () => clearInterval(interval);
};

// 示例7: 在组件中使用
export const useCurrentVideo = () => {
  const [currentVideo, setCurrentVideo] = useState<any>(null);
  const [currentIndex, setCurrentIndex] = useState(0);
  const [comments, setComments] = useState<any[]>([]);

  useEffect(() => {
    const updateCurrentVideo = () => {
      const video = getCurrentPlayingVideo();
      const index = getCurrentVideoIndex();
      const videoComments = getCurrentVideoComments();

      setCurrentVideo(video);
      setCurrentIndex(index);
      setComments(videoComments);
    };

    // 初始获取
    updateCurrentVideo();

    // 定期更新
    const interval = setInterval(updateCurrentVideo, 1000);

    return () => clearInterval(interval);
  }, []);

  return { currentVideo, currentIndex, comments };
};


// 示例8: 在全局范围内使用（比如在控制台中）
if (typeof window !== 'undefined') {
  // 将方法挂载到window对象上，方便在控制台中调试
  (window as any).getCurrentPlayingVideo = getCurrentPlayingVideo;
  (window as any).getCurrentVideoIndex = getCurrentVideoIndex;
  (window as any).scrollToVideo = scrollToVideo;
  (window as any).forceCheckCurrentVideo = forceCheckCurrentVideo;
  (window as any).getCurrentVideoComments = getCurrentVideoComments;

  console.log('视频控制方法已挂载到window对象:');
  console.log('- window.getCurrentPlayingVideo() - 获取当前播放的视频数据');
  console.log('- window.getCurrentVideoIndex() - 获取当前播放视频的索引');
  console.log('- window.scrollToVideo(index) - 跳转到指定视频');
  console.log('- window.forceCheckCurrentVideo() - 强制检测当前播放视频');
  console.log('- window.getCurrentVideoComments() - 获取当前视频的评论数据');
}
